<?php 
class ControllerProductCategory extends Controller {  
	
  public function index() 
  {              
		$this->language->load('product/category');
    $this->load->model('catalog/category');
    $this->load->model('tool/seo_url');
    $this->load->model('tool/image'); 
	
    // generate breadcrumb
		$this->document->breadcrumbs = array();

   	$this->document->breadcrumbs[] = array(
      	'href'      => HTTP_SERVER . 'index.php?route=common/home',
       	'text'      => $this->language->get('text_home'),
       	'separator' => FALSE
   	);
    
    if (isset($this->request->get['path'])) 
    {
      $path = '';
      $parts = explode('_', $this->request->get['path']);
    
      foreach ($parts as $path_id) 
      {
        $category_info = $this->model_catalog_category->getCategory($path_id);
        
        if ($category_info) 
        {
          if (!$path) 
          {
            $path = $path_id;
          } 
          else 
          {
            $path .= '_' . $path_id;
          }

           $this->document->breadcrumbs[] = array(
             'href'      => $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/category&path=' . $path),
             'text'      => $category_info['name'],
            'separator' => $this->language->get('text_separator')
          );
        }
      }    
    
      $category_id = array_pop($parts);
    } 
    else 
    {
      $category_id = 0;
    }	
    
		// ----------------------------------------------------
    // check if user want to see main cat.
    if (!isset($this->request->get['path']))
    {                      
      // get all categories      
      $this->data['main_cats'] = $this->model_catalog_category->getCategories(0); 
      for ($i = 0; $i < count($this->data['main_cats']); $i++) 
      {
        $this->data['main_cats'][$i]['pic'] = $this->model_tool_image->resize($this->data['main_cats'][$i]['image'], 310, 330);
        $this->data['main_cats'][$i]['href'] = $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/category&path=' . $this->data['main_cats'][$i]['category_id']);
      }
      
      // get new products
      $this->load->model('catalog/product');
      $this->load->helper('text');
      
      $this->data['new_products'] = $this->model_catalog_product->getProductsOnCondition(array('new' => true, 'limit' => 3));    
      for ($i = 0; $i < count($this->data['new_products']); $i++) 
      {
        $this->data['new_products'][$i]['pic'] = $this->model_tool_image->resize($this->data['new_products'][$i]['image'], 312, 190);
        $this->data['new_products'][$i]['description'] = word_limiter(strip_tags(html_entity_decode($this->data['new_products'][$i]['description'])), 10);
        $this->data['new_products'][$i]['href'] = $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/product&product_id=' . $this->data['new_products'][$i]['product_id']);
      }
      
      $this->template = $this->config->get('config_template') . '/template/product/category_main.tpl';
    }
    else
    {      
      $parts = explode('_', $this->request->get['path']);

      // in case user want to check sub cat.
      if (count($parts) == 1)
      {
        // get all sub cat.
        $this->data['sub_cats'] = $this->model_catalog_category->getCategories($parts[0]); 
        for ($i = 0; $i < count($this->data['sub_cats']); $i++) 
        {
          $this->data['sub_cats'][$i]['pic'] = $this->model_tool_image->resize($this->data['sub_cats'][$i]['image'], 310, 330);
          $cat_path = $parts[0].'_'.$this->data['sub_cats'][$i]['category_id'];
          $this->data['sub_cats'][$i]['href'] = $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/category/listing&path=' . $cat_path);
        }
        
        $this->template = $this->config->get('config_template') . '/template/product/category_main2.tpl';  
      }
    }
     
    $this->children = array(
      'common/footer',
      'common/header'
    );
    
    // add scripts & styles
    $this->document->addScript('catalog/view/javascript/jquery/jquery.easing.1.3.js');
    $this->document->addScript('catalog/view/javascript/jquery/jquery.timers-1.1.2.js'); 
    $this->document->addScript('catalog/view/javascript/jquery/transify.js');
    $this->document->addScript('catalog/view/javascript/jquery/jquery.cf.js');
    $this->document->addStyle('catalog/view/theme/' . $this->config->get('config_template') . '/stylesheet/home.css');  
    
    $this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));                
  }
  
  
  /**
  * Display the product list 
  * 
  */
  public function listing()
  {
    $this->load->model('catalog/category');
    $this->load->model('catalog/product');
    $this->load->model('tool/seo_url');
    $this->load->model('tool/image');
    $this->load->helper('text'); 
    
    // generate breadcrumb
    $this->document->breadcrumbs = array();

    $this->document->breadcrumbs[] = array(
      'href'      => HTTP_SERVER . 'index.php?route=common/home',
      'text'      => $this->language->get('text_home'),
      'separator' => FALSE
    );
    
    if (isset($this->request->get['path'])) 
    {
      $path = '';
      $parts = explode('_', $this->request->get['path']);
    
      foreach ($parts as $path_id) 
      {
        $category_info = $this->model_catalog_category->getCategory($path_id);
        
        if ($category_info) 
        {
          if (!$path) 
          {
            $path = $path_id;
          } 
          else 
          {
            $path .= '_' . $path_id;
          }

           $this->document->breadcrumbs[] = array(
             'href'      => $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/category&path=' . $path),
             'text'      => $category_info['name'],
            'separator' => $this->language->get('text_separator')
          );
        }
      }    
    
      $category_id = array_pop($parts);
    } 
    else 
    {
      $category_id = 0;
    }  
    
    // ------------------------------------------------------------------
    // get products list
    $page = isset($this->request->get['page']) ? (int)$this->request->get['page'] : 1;
    
    $product_total = $this->model_catalog_product->getTotalProductsByCategoryId($category_id);
    $this->data['products'] = array();
    
    if ($product_total)
    {      
      $this->data['products'] = $this->model_catalog_product->getProductsByCategoryId($category_id, 'p.sort_order', 'ASC', ($page - 1) * $this->config->get('config_catalog_limit'), $this->config->get('config_catalog_limit'));  
      
      for ($i = 0; $i < count($this->data['products']); $i++) 
      {
        $this->data['products'][$i]['pic'] = $this->model_tool_image->resize($this->data['products'][$i]['image'], 312, 190);
        $this->data['products'][$i]['description'] = word_limiter(strip_tags(html_entity_decode($this->data['products'][$i]['description'])), 10);
        $this->data['products'][$i]['href'] = $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/product&product_id=' . $this->data['products'][$i]['product_id']);
      }
    }
    
    // ------------------------------------------------------------------
    // create pagination
    $pagination = new Pagination();
    $pagination->total = $product_total;
    $pagination->page = $page;
    $pagination->limit = $this->config->get('config_catalog_limit');
    $pagination->text = $this->language->get('text_pagination');
    $pagination->url = $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/category/listing&path=' . $this->request->get['path'] . '&page={page}');  
    $this->data['pagination'] = $pagination->render();
        
    $this->template = $this->config->get('config_template') . '/template/product/product_list.tpl';
    
    $this->children = array(
      'common/footer',
      'common/header'
    );
    
    // add scripts & styles
    $this->document->addScript('catalog/view/javascript/jquery/jquery.galleryview-1.1.js');
    $this->document->addScript('catalog/view/javascript/jquery/jquery.easing.1.3.js');
    $this->document->addScript('catalog/view/javascript/jquery/jquery.timers-1.1.2.js'); 
    $this->document->addScript('catalog/view/javascript/jquery/transify.js');
    $this->document->addScript('catalog/view/javascript/jquery/jquery.cf.js');
    $this->document->addStyle('catalog/view/theme/' . $this->config->get('config_template') . '/stylesheet/home.css');
    
    $this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));
  }
}
?>